home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / info / cl-5.z / cl-5
Encoding:
GNU Info File  |  1998-10-28  |  42.9 KB  |  918 lines

  1. This is Info file ../info/cl, produced by Makeinfo-1.64 from the input
  2. file cl.texi.
  3.  
  4.    This file documents the GNU Emacs Common Lisp emulation package.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the section entitled "GNU General Public License" is included
  15. exactly as in the original, and provided that the entire resulting
  16. derived work is distributed under the terms of a permission notice
  17. identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the section entitled "GNU General Public License"
  22. may be included in a translation approved by the author instead of in
  23. the original English.
  24.  
  25. 
  26. File: cl,  Node: Structures,  Next: Assertions,  Prev: Hash Tables,  Up: Top
  27.  
  28. Structures
  29. **********
  30.  
  31. The Common Lisp "structure" mechanism provides a general way to define
  32. data types similar to C's `struct' types.  A structure is a Lisp object
  33. containing some number of "slots", each of which can hold any Lisp data
  34. object.  Functions are provided for accessing and setting the slots,
  35. creating or copying structure objects, and recognizing objects of a
  36. particular structure type.
  37.  
  38.    In true Common Lisp, each structure type is a new type distinct from
  39. all existing Lisp types.  Since the underlying Emacs Lisp system
  40. provides no way to create new distinct types, this package implements
  41. structures as vectors (or lists upon request) with a special "tag"
  42. symbol to identify them.
  43.  
  44.  - Special Form: defstruct NAME SLOTS...
  45.      The `defstruct' form defines a new structure type called NAME,
  46.      with the specified SLOTS.  (The SLOTS may begin with a string
  47.      which documents the structure type.) In the simplest case, NAME
  48.      and each of the SLOTS are symbols.  For example,
  49.  
  50.           (defstruct person name age sex)
  51.  
  52.      defines a struct type called `person' which contains three slots.
  53.      Given a `person' object P, you can access those slots by calling
  54.      `(person-name P)', `(person-age P)', and `(person-sex P)'.  You
  55.      can also change these slots by using `setf' on any of these place
  56.      forms:
  57.  
  58.           (incf (person-age birthday-boy))
  59.  
  60.      You can create a new `person' by calling `make-person', which
  61.      takes keyword arguments `:name', `:age', and `:sex' to specify the
  62.      initial values of these slots in the new object.  (Omitting any of
  63.      these arguments leaves the corresponding slot "undefined,"
  64.      according to the Common Lisp standard; in Emacs Lisp, such
  65.      uninitialized slots are filled with `nil'.)
  66.  
  67.      Given a `person', `(copy-person P)' makes a new object of the same
  68.      type whose slots are `eq' to those of P.
  69.  
  70.      Given any Lisp object X, `(person-p X)' returns true if X looks
  71.      like a `person', false otherwise.  (Again, in Common Lisp this
  72.      predicate would be exact; in Emacs Lisp the best it can do is
  73.      verify that X is a vector of the correct length which starts with
  74.      the correct tag symbol.)
  75.  
  76.      Accessors like `person-name' normally check their arguments
  77.      (effectively using `person-p') and signal an error if the argument
  78.      is the wrong type.  This check is affected by `(optimize (safety
  79.      ...))' declarations.  Safety level 1, the default, uses a somewhat
  80.      optimized check that will detect all incorrect arguments, but may
  81.      use an uninformative error message (e.g., "expected a vector"
  82.      instead of "expected a `person'").  Safety level 0 omits all
  83.      checks except as provided by the underlying `aref' call; safety
  84.      levels 2 and 3 do rigorous checking that will always print a
  85.      descriptive error message for incorrect inputs.  *Note
  86.      Declarations::.
  87.  
  88.           (setq dave (make-person :name "Dave" :sex 'male))
  89.                => [cl-struct-person "Dave" nil male]
  90.           (setq other (copy-person dave))
  91.                => [cl-struct-person "Dave" nil male]
  92.           (eq dave other)
  93.                => nil
  94.           (eq (person-name dave) (person-name other))
  95.                => t
  96.           (person-p dave)
  97.                => t
  98.           (person-p [1 2 3 4])
  99.                => nil
  100.           (person-p "Bogus")
  101.                => nil
  102.           (person-p '[cl-struct-person counterfeit person object])
  103.                => t
  104.  
  105.      In general, NAME is either a name symbol or a list of a name
  106.      symbol followed by any number of "struct options"; each SLOT is
  107.      either a slot symbol or a list of the form `(SLOT-NAME
  108.      DEFAULT-VALUE SLOT-OPTIONS...)'.  The DEFAULT-VALUE is a Lisp form
  109.      which is evaluated any time an instance of the structure type is
  110.      created without specifying that slot's value.
  111.  
  112.      Common Lisp defines several slot options, but the only one
  113.      implemented in this package is `:read-only'.  A non-`nil' value
  114.      for this option means the slot should not be `setf'-able; the
  115.      slot's value is determined when the object is created and does not
  116.      change afterward.
  117.  
  118.           (defstruct person
  119.             (name nil :read-only t)
  120.             age
  121.             (sex 'unknown))
  122.  
  123.      Any slot options other than `:read-only' are ignored.
  124.  
  125.      For obscure historical reasons, structure options take a different
  126.      form than slot options.  A structure option is either a keyword
  127.      symbol, or a list beginning with a keyword symbol possibly followed
  128.      by arguments.  (By contrast, slot options are key-value pairs not
  129.      enclosed in lists.)
  130.  
  131.           (defstruct (person (:constructor create-person)
  132.                              (:type list)
  133.                              :named)
  134.             name age sex)
  135.  
  136.      The following structure options are recognized.
  137.  
  138.     `:conc-name'
  139.           The argument is a symbol whose print name is used as the
  140.           prefix for the names of slot accessor functions.  The default
  141.           is the name of the struct type followed by a hyphen.  The
  142.           option `(:conc-name p-)' would change this prefix to `p-'.
  143.           Specifying `nil' as an argument means no prefix, so that the
  144.           slot names themselves are used to name the accessor functions.
  145.  
  146.     `:constructor'
  147.           In the simple case, this option takes one argument which is an
  148.           alternate name to use for the constructor function.  The
  149.           default is `make-NAME', e.g., `make-person'.  The above
  150.           example changes this to `create-person'.  Specifying `nil' as
  151.           an argument means that no standard constructor should be
  152.           generated at all.
  153.  
  154.           In the full form of this option, the constructor name is
  155.           followed by an arbitrary argument list.  *Note Program
  156.           Structure::, for a description of the format of Common Lisp
  157.           argument lists.  All options, such as `&rest' and `&key', are
  158.           supported.  The argument names should match the slot names;
  159.           each slot is initialized from the corresponding argument.
  160.           Slots whose names do not appear in the argument list are
  161.           initialized based on the DEFAULT-VALUE in their slot
  162.           descriptor.  Also, `&optional' and `&key' arguments which
  163.           don't specify defaults take their defaults from the slot
  164.           descriptor.  It is legal to include arguments which don't
  165.           correspond to slot names; these are useful if they are
  166.           referred to in the defaults for optional, keyword, or `&aux'
  167.           arguments which *do* correspond to slots.
  168.  
  169.           You can specify any number of full-format `:constructor'
  170.           options on a structure.  The default constructor is still
  171.           generated as well unless you disable it with a simple-format
  172.           `:constructor' option.
  173.  
  174.                (defstruct
  175.                 (person
  176.                  (:constructor nil)   ; no default constructor
  177.                  (:constructor new-person (name sex &optional (age 0)))
  178.                  (:constructor new-hound (&key (name "Rover")
  179.                                                (dog-years 0)
  180.                                           &aux (age (* 7 dog-years))
  181.                                                (sex 'canine))))
  182.                 name age sex)
  183.  
  184.           The first constructor here takes its arguments positionally
  185.           rather than by keyword.  (In official Common Lisp
  186.           terminology, constructors that work By Order of Arguments
  187.           instead of by keyword are called "BOA constructors."  No, I'm
  188.           not making this up.)  For example, `(new-person "Jane"
  189.           'female)' generates a person whose slots are `"Jane"', 0, and
  190.           `female', respectively.
  191.  
  192.           The second constructor takes two keyword arguments, `:name',
  193.           which initializes the `name' slot and defaults to `"Rover"',
  194.           and `:dog-years', which does not itself correspond to a slot
  195.           but which is used to initialize the `age' slot.  The `sex'
  196.           slot is forced to the symbol `canine' with no syntax for
  197.           overriding it.
  198.  
  199.     `:copier'
  200.           The argument is an alternate name for the copier function for
  201.           this type.  The default is `copy-NAME'.  `nil' means not to
  202.           generate a copier function.  (In this implementation, all
  203.           copier functions are simply synonyms for `copy-sequence'.)
  204.  
  205.     `:predicate'
  206.           The argument is an alternate name for the predicate which
  207.           recognizes objects of this type.  The default is `NAME-p'.
  208.           `nil' means not to generate a predicate function.  (If the
  209.           `:type' option is used without the `:named' option, no
  210.           predicate is ever generated.)
  211.  
  212.           In true Common Lisp, `typep' is always able to recognize a
  213.           structure object even if `:predicate' was used.  In this
  214.           package, `typep' simply looks for a function called
  215.           `TYPENAME-p', so it will work for structure types only if
  216.           they used the default predicate name.
  217.  
  218.     `:include'
  219.           This option implements a very limited form of C++-style
  220.           inheritance.  The argument is the name of another structure
  221.           type previously created with `defstruct'.  The effect is to
  222.           cause the new structure type to inherit all of the included
  223.           structure's slots (plus, of course, any new slots described
  224.           by this struct's slot descriptors).  The new structure is
  225.           considered a "specialization" of the included one.  In fact,
  226.           the predicate and slot accessors for the included type will
  227.           also accept objects of the new type.
  228.  
  229.           If there are extra arguments to the `:include' option after
  230.           the included-structure name, these options are treated as
  231.           replacement slot descriptors for slots in the included
  232.           structure, possibly with modified default values.  Borrowing
  233.           an example from Steele:
  234.  
  235.                (defstruct person name (age 0) sex)
  236.                     => person
  237.                (defstruct (astronaut (:include person (age 45)))
  238.                  helmet-size
  239.                  (favorite-beverage 'tang))
  240.                     => astronaut
  241.                
  242.                (setq joe (make-person :name "Joe"))
  243.                     => [cl-struct-person "Joe" 0 nil]
  244.                (setq buzz (make-astronaut :name "Buzz"))
  245.                     => [cl-struct-astronaut "Buzz" 45 nil nil tang]
  246.                
  247.                (list (person-p joe) (person-p buzz))
  248.                     => (t t)
  249.                (list (astronaut-p joe) (astronaut-p buzz))
  250.                     => (nil t)
  251.                
  252.                (person-name buzz)
  253.                     => "Buzz"
  254.                (astronaut-name joe)
  255.                     => error: "astronaut-name accessing a non-astronaut"
  256.  
  257.           Thus, if `astronaut' is a specialization of `person', then
  258.           every `astronaut' is also a `person' (but not the other way
  259.           around).  Every `astronaut' includes all the slots of a
  260.           `person', plus extra slots that are specific to astronauts.
  261.           Operations that work on people (like `person-name') work on
  262.           astronauts just like other people.
  263.  
  264.     `:print-function'
  265.           In full Common Lisp, this option allows you to specify a
  266.           function which is called to print an instance of the
  267.           structure type.  The Emacs Lisp system offers no hooks into
  268.           the Lisp printer which would allow for such a feature, so
  269.           this package simply ignores `:print-function'.
  270.  
  271.     `:type'
  272.           The argument should be one of the symbols `vector' or `list'.
  273.           This tells which underlying Lisp data type should be used to
  274.           implement the new structure type.  Vectors are used by
  275.           default, but `(:type list)' will cause structure objects to
  276.           be stored as lists instead.
  277.  
  278.           The vector representation for structure objects has the
  279.           advantage that all structure slots can be accessed quickly,
  280.           although creating vectors is a bit slower in Emacs Lisp.
  281.           Lists are easier to create, but take a relatively long time
  282.           accessing the later slots.
  283.  
  284.     `:named'
  285.           This option, which takes no arguments, causes a
  286.           characteristic "tag" symbol to be stored at the front of the
  287.           structure object.  Using `:type' without also using `:named'
  288.           will result in a structure type stored as plain vectors or
  289.           lists with no identifying features.
  290.  
  291.           The default, if you don't specify `:type' explicitly, is to
  292.           use named vectors.  Therefore, `:named' is only useful in
  293.           conjunction with `:type'.
  294.  
  295.                (defstruct (person1) name age sex)
  296.                (defstruct (person2 (:type list) :named) name age sex)
  297.                (defstruct (person3 (:type list)) name age sex)
  298.                
  299.                (setq p1 (make-person1))
  300.                     => [cl-struct-person1 nil nil nil]
  301.                (setq p2 (make-person2))
  302.                     => (person2 nil nil nil)
  303.                (setq p3 (make-person3))
  304.                     => (nil nil nil)
  305.                
  306.                (person1-p p1)
  307.                     => t
  308.                (person2-p p2)
  309.                     => t
  310.                (person3-p p3)
  311.                     => error: function person3-p undefined
  312.  
  313.           Since unnamed structures don't have tags, `defstruct' is not
  314.           able to make a useful predicate for recognizing them.  Also,
  315.           accessors like `person3-name' will be generated but they will
  316.           not be able to do any type checking.  The `person3-name'
  317.           function, for example, will simply be a synonym for `car' in
  318.           this case.  By contrast, `person2-name' is able to verify
  319.           that its argument is indeed a `person2' object before
  320.           proceeding.
  321.  
  322.     `:initial-offset'
  323.           The argument must be a nonnegative integer.  It specifies a
  324.           number of slots to be left "empty" at the front of the
  325.           structure.  If the structure is named, the tag appears at the
  326.           specified position in the list or vector; otherwise, the first
  327.           slot appears at that position.  Earlier positions are filled
  328.           with `nil' by the constructors and ignored otherwise.  If the
  329.           type `:include's another type, then `:initial-offset'
  330.           specifies a number of slots to be skipped between the last
  331.           slot of the included type and the first new slot.
  332.  
  333.    Except as noted, the `defstruct' facility of this package is
  334. entirely compatible with that of Common Lisp.
  335.  
  336. 
  337. File: cl,  Node: Assertions,  Next: Efficiency Concerns,  Prev: Structures,  Up: Top
  338.  
  339. Assertions and Errors
  340. *********************
  341.  
  342. This section describes two macros that test "assertions", i.e.,
  343. conditions which must be true if the program is operating correctly.
  344. Assertions never add to the behavior of a Lisp program; they simply
  345. make "sanity checks" to make sure everything is as it should be.
  346.  
  347.    If the optimization property `speed' has been set to 3, and `safety'
  348. is less than 3, then the byte-compiler will optimize away the following
  349. assertions.  Because assertions might be optimized away, it is a bad
  350. idea for them to include side-effects.
  351.  
  352.  - Special Form: assert TEST-FORM [SHOW-ARGS STRING ARGS...]
  353.      This form verifies that TEST-FORM is true (i.e., evaluates to a
  354.      non-`nil' value).  If so, it returns `nil'.  If the test is not
  355.      satisfied, `assert' signals an error.
  356.  
  357.      A default error message will be supplied which includes TEST-FORM.
  358.      You can specify a different error message by including a STRING
  359.      argument plus optional extra arguments.  Those arguments are simply
  360.      passed to `error' to signal the error.
  361.  
  362.      If the optional second argument SHOW-ARGS is `t' instead of `nil',
  363.      then the error message (with or without STRING) will also include
  364.      all non-constant arguments of the top-level FORM.  For example:
  365.  
  366.           (assert (> x 10) t "x is too small: %d")
  367.  
  368.      This usage of SHOW-ARGS is an extension to Common Lisp.  In true
  369.      Common Lisp, the second argument gives a list of PLACES which can
  370.      be `setf''d by the user before continuing from the error.  Since
  371.      Emacs Lisp does not support continuable errors, it makes no sense
  372.      to specify PLACES.
  373.  
  374.  - Special Form: check-type FORM TYPE [STRING]
  375.      This form verifies that FORM evaluates to a value of type TYPE.
  376.      If so, it returns `nil'.  If not, `check-type' signals a
  377.      `wrong-type-argument' error.  The default error message lists the
  378.      erroneous value along with TYPE and FORM themselves.  If STRING is
  379.      specified, it is included in the error message in place of TYPE.
  380.      For example:
  381.  
  382.           (check-type x (integer 1 *) "a positive integer")
  383.  
  384.      *Note Type Predicates::, for a description of the type specifiers
  385.      that may be used for TYPE.
  386.  
  387.      Note that in Common Lisp, the first argument to `check-type' must
  388.      be a PLACE suitable for use by `setf', because `check-type'
  389.      signals a continuable error that allows the user to modify PLACE.
  390.  
  391.    The following error-related macro is also defined:
  392.  
  393.  - Special Form: ignore-errors FORMS...
  394.      This executes FORMS exactly like a `progn', except that errors are
  395.      ignored during the FORMS.  More precisely, if an error is signaled
  396.      then `ignore-errors' immediately aborts execution of the FORMS and
  397.      returns `nil'.  If the FORMS complete successfully, `ignore-errors'
  398.      returns the result of the last FORM.
  399.  
  400. 
  401. File: cl,  Node: Efficiency Concerns,  Next: Common Lisp Compatibility,  Prev: Assertions,  Up: Top
  402.  
  403. Efficiency Concerns
  404. *******************
  405.  
  406. Macros
  407. ======
  408.  
  409. Many of the advanced features of this package, such as `defun*',
  410. `loop', and `setf', are implemented as Lisp macros.  In byte-compiled
  411. code, these complex notations will be expanded into equivalent Lisp
  412. code which is simple and efficient.  For example, the forms
  413.  
  414.      (incf i n)
  415.      (push x (car p))
  416.  
  417. are expanded at compile-time to the Lisp forms
  418.  
  419.      (setq i (+ i n))
  420.      (setcar p (cons x (car p)))
  421.  
  422. which are the most efficient ways of doing these respective operations
  423. in Lisp.  Thus, there is no performance penalty for using the more
  424. readable `incf' and `push' forms in your compiled code.
  425.  
  426.    *Interpreted* code, on the other hand, must expand these macros
  427. every time they are executed.  For this reason it is strongly
  428. recommended that code making heavy use of macros be compiled.  (The
  429. features labeled "Special Form" instead of "Function" in this manual
  430. are macros.)  A loop using `incf' a hundred times will execute
  431. considerably faster if compiled, and will also garbage-collect less
  432. because the macro expansion will not have to be generated, used, and
  433. thrown away a hundred times.
  434.  
  435.    You can find out how a macro expands by using the `cl-prettyexpand'
  436. function.
  437.  
  438.  - Function: cl-prettyexpand FORM &optional FULL
  439.      This function takes a single Lisp form as an argument and inserts
  440.      a nicely formatted copy of it in the current buffer (which must be
  441.      in Lisp mode so that indentation works properly).  It also expands
  442.      all Lisp macros which appear in the form.  The easiest way to use
  443.      this function is to go to the `*scratch*' buffer and type, say,
  444.  
  445.           (cl-prettyexpand '(loop for x below 10 collect x))
  446.  
  447.      and type `C-x C-e' immediately after the closing parenthesis; the
  448.      expansion
  449.  
  450.           (block nil
  451.             (let* ((x 0)
  452.                    (G1004 nil))
  453.               (while (< x 10)
  454.                 (setq G1004 (cons x G1004))
  455.                 (setq x (+ x 1)))
  456.               (nreverse G1004)))
  457.  
  458.      will be inserted into the buffer.  (The `block' macro is expanded
  459.      differently in the interpreter and compiler, so `cl-prettyexpand'
  460.      just leaves it alone.  The temporary variable `G1004' was created
  461.      by `gensym'.)
  462.  
  463.      If the optional argument FULL is true, then *all* macros are
  464.      expanded, including `block', `eval-when', and compiler macros.
  465.      Expansion is done as if FORM were a top-level form in a file being
  466.      compiled.  For example,
  467.  
  468.           (cl-prettyexpand '(pushnew 'x list))
  469.                -| (setq list (adjoin 'x list))
  470.           (cl-prettyexpand '(pushnew 'x list) t)
  471.                -| (setq list (if (memq 'x list) list (cons 'x list)))
  472.           (cl-prettyexpand '(caddr (member* 'a list)) t)
  473.                -| (car (cdr (cdr (memq 'a list))))
  474.  
  475.      Note that `adjoin', `caddr', and `member*' all have built-in
  476.      compiler macros to optimize them in common cases.
  477.  
  478.  
  479. Error Checking
  480. ==============
  481.  
  482. Common Lisp compliance has in general not been sacrificed for the sake
  483. of efficiency.  A few exceptions have been made for cases where
  484. substantial gains were possible at the expense of marginal
  485. incompatibility.  One example is the use of `memq' (which is treated
  486. very efficiently by the byte-compiler) to scan for keyword arguments;
  487. this can become confused in rare cases when keyword symbols are used as
  488. both keywords and data values at once.  This is extremely unlikely to
  489. occur in practical code, and the use of `memq' allows functions with
  490. keyword arguments to be nearly as fast as functions that use
  491. `&optional' arguments.
  492.  
  493.    The Common Lisp standard (as embodied in Steele's book) uses the
  494. phrase "it is an error if" to indicate a situation which is not
  495. supposed to arise in complying programs; implementations are strongly
  496. encouraged but not required to signal an error in these situations.
  497. This package sometimes omits such error checking in the interest of
  498. compactness and efficiency.  For example, `do' variable specifiers are
  499. supposed to be lists of one, two, or three forms; extra forms are
  500. ignored by this package rather than signaling a syntax error.  The
  501. `endp' function is simply a synonym for `null' in this package.
  502. Functions taking keyword arguments will accept an odd number of
  503. arguments, treating the trailing keyword as if it were followed by the
  504. value `nil'.
  505.  
  506.    Argument lists (as processed by `defun*' and friends) *are* checked
  507. rigorously except for the minor point just mentioned; in particular,
  508. keyword arguments are checked for validity, and `&allow-other-keys' and
  509. `:allow-other-keys' are fully implemented.  Keyword validity checking
  510. is slightly time consuming (though not too bad in byte-compiled code);
  511. you can use `&allow-other-keys' to omit this check.  Functions defined
  512. in this package such as `find' and `member*' do check their keyword
  513. arguments for validity.
  514.  
  515.  
  516. Optimizing Compiler
  517. ===================
  518.  
  519. The byte-compiler that comes with Emacs 18 normally fails to expand
  520. macros that appear in top-level positions in the file (i.e., outside of
  521. `defun's or other enclosing forms).  This would have disastrous
  522. consequences to programs that used such top-level macros as `defun*',
  523. `eval-when', and `defstruct'.  To work around this problem, the "CL"
  524. package patches the Emacs 18 compiler to expand top-level macros.  This
  525. patch will apply to your own macros, too, if they are used in a
  526. top-level context.  The patch will not harm versions of the Emacs 18
  527. compiler which have already had a similar patch applied, nor will it
  528. affect the optimizing Emacs 19 byte-compiler written by Jamie Zawinski
  529. and Hallvard Furuseth.  The patch is applied to the byte compiler's
  530. code in Emacs' memory, *not* to the `bytecomp.elc' file stored on disk.
  531.  
  532.    The Emacs 19 compiler (for Emacs 18) is available from various Emacs
  533. Lisp archive sites such as `archive.cis.ohio-state.edu'.  Its use is
  534. highly recommended; many of the Common Lisp macros emit code which can
  535. be improved by optimization.  In particular, `block's (whether explicit
  536. or implicit in constructs like `defun*' and `loop') carry a fair
  537. run-time penalty; the optimizing compiler removes `block's which are
  538. not actually referenced by `return' or `return-from' inside the block.
  539.  
  540. 
  541. File: cl,  Node: Common Lisp Compatibility,  Next: Old CL Compatibility,  Prev: Efficiency Concerns,  Up: Top
  542.  
  543. Common Lisp Compatibility
  544. *************************
  545.  
  546. Following is a list of all known incompatibilities between this package
  547. and Common Lisp as documented in Steele (2nd edition).
  548.  
  549.    Certain function names, such as `member', `assoc', and `floor', were
  550. already taken by (incompatible) Emacs Lisp functions; this package
  551. appends `*' to the names of its Common Lisp versions of these functions.
  552.  
  553.    The word `defun*' is required instead of `defun' in order to use
  554. extended Common Lisp argument lists in a function.  Likewise,
  555. `defmacro*' and `function*' are versions of those forms which
  556. understand full-featured argument lists.  The `&whole' keyword does not
  557. work in `defmacro' argument lists (except inside recursive argument
  558. lists).
  559.  
  560.    In order to allow an efficient implementation, keyword arguments use
  561. a slightly cheesy parser which may be confused if a keyword symbol is
  562. passed as the *value* of another keyword argument.  (Specifically,
  563. `(memq :KEYWORD REST-OF-ARGUMENTS)' is used to scan for `:KEYWORD'
  564. among the supplied keyword arguments.)
  565.  
  566.    The `eql' and `equal' predicates do not distinguish between IEEE
  567. floating-point plus and minus zero.  The `equalp' predicate has several
  568. differences with Common Lisp; *note Predicates::..
  569.  
  570.    The `setf' mechanism is entirely compatible, except that
  571. setf-methods return a list of five values rather than five values
  572. directly.  Also, the new "`setf' function" concept (typified by `(defun
  573. (setf foo) ...)') is not implemented.
  574.  
  575.    The `do-all-symbols' form is the same as `do-symbols' with no
  576. OBARRAY argument.  In Common Lisp, this form would iterate over all
  577. symbols in all packages.  Since Emacs obarrays are not a first-class
  578. package mechanism, there is no way for `do-all-symbols' to locate any
  579. but the default obarray.
  580.  
  581.    The `loop' macro is complete except that `loop-finish' and type
  582. specifiers are unimplemented.
  583.  
  584.    The multiple-value return facility treats lists as multiple values,
  585. since Emacs Lisp cannot support multiple return values directly.  The
  586. macros will be compatible with Common Lisp if `values' or `values-list'
  587. is always used to return to a `multiple-value-bind' or other
  588. multiple-value receiver; if `values' is used without
  589. `multiple-value-...' or vice-versa the effect will be different from
  590. Common Lisp.
  591.  
  592.    Many Common Lisp declarations are ignored, and others match the
  593. Common Lisp standard in concept but not in detail.  For example, local
  594. `special' declarations, which are purely advisory in Emacs Lisp, do not
  595. rigorously obey the scoping rules set down in Steele's book.
  596.  
  597.    The variable `*gensym-counter*' starts out with a pseudo-random
  598. value rather than with zero.  This is to cope with the fact that
  599. generated symbols become interned when they are written to and loaded
  600. back from a file.
  601.  
  602.    The `defstruct' facility is compatible, except that structures are
  603. of type `:type vector :named' by default rather than some special,
  604. distinct type.  Also, the `:type' slot option is ignored.
  605.  
  606.    The second argument of `check-type' is treated differently.
  607.  
  608. 
  609. File: cl,  Node: Old CL Compatibility,  Next: Porting Common Lisp,  Prev: Common Lisp Compatibility,  Up: Top
  610.  
  611. Old CL Compatibility
  612. ********************
  613.  
  614. Following is a list of all known incompatibilities between this package
  615. and the older Quiroz `cl.el' package.
  616.  
  617.    This package's emulation of multiple return values in functions is
  618. incompatible with that of the older package.  That package attempted to
  619. come as close as possible to true Common Lisp multiple return values;
  620. unfortunately, it could not be 100% reliable and so was prone to
  621. occasional surprises if used freely.  This package uses a simpler
  622. method, namely replacing multiple values with lists of values, which is
  623. more predictable though more noticeably different from Common Lisp.
  624.  
  625.    The `defkeyword' form and `keywordp' function are not implemented in
  626. this package.
  627.  
  628.    The `member', `floor', `ceiling', `truncate', `round', `mod', and
  629. `rem' functions are suffixed by `*' in this package to avoid collision
  630. with existing functions in Emacs 18 or Emacs 19.  The older package
  631. simply redefined these functions, overwriting the built-in meanings and
  632. causing serious portability problems with Emacs 19.  (Some more recent
  633. versions of the Quiroz package changed the names to `cl-member', etc.;
  634. this package defines the latter names as aliases for `member*', etc.)
  635.  
  636.    Certain functions in the old package which were buggy or inconsistent
  637. with the Common Lisp standard are incompatible with the conforming
  638. versions in this package.  For example, `eql' and `member' were
  639. synonyms for `eq' and `memq' in that package, `setf' failed to preserve
  640. correct order of evaluation of its arguments, etc.
  641.  
  642.    Finally, unlike the older package, this package is careful to prefix
  643. all of its internal names with `cl-'.  Except for a few functions which
  644. are explicitly defined as additional features (such as `floatp-safe'
  645. and `letf'), this package does not export any non-`cl-' symbols which
  646. are not also part of Common Lisp.
  647.  
  648.  
  649. The `cl-compat' package
  650. =======================
  651.  
  652. The "CL" package includes emulations of some features of the old
  653. `cl.el', in the form of a compatibility package `cl-compat'.  To use
  654. it, put `(require 'cl-compat)' in your program.
  655.  
  656.    The old package defined a number of internal routines without `cl-'
  657. prefixes or other annotations.  Call to these routines may have crept
  658. into existing Lisp code.  `cl-compat' provides emulations of the
  659. following internal routines: `pair-with-newsyms', `zip-lists',
  660. `unzip-lists', `reassemble-arglists', `duplicate-symbols-p',
  661. `safe-idiv'.
  662.  
  663.    Some `setf' forms translated into calls to internal functions that
  664. user code might call directly.  The functions `setnth', `setnthcdr',
  665. and `setelt' fall in this category; they are defined by `cl-compat',
  666. but the best fix is to change to use `setf' properly.
  667.  
  668.    The `cl-compat' file defines the keyword functions `keywordp',
  669. `keyword-of', and `defkeyword', which are not defined by the new "CL"
  670. package because the use of keywords as data is discouraged.
  671.  
  672.    The `build-klist' mechanism for parsing keyword arguments is
  673. emulated by `cl-compat'; the `with-keyword-args' macro is not, however,
  674. and in any case it's best to change to use the more natural keyword
  675. argument processing offered by `defun*'.
  676.  
  677.    Multiple return values are treated differently by the two Common
  678. Lisp packages.  The old package's method was more compatible with true
  679. Common Lisp, though it used heuristics that caused it to report
  680. spurious multiple return values in certain cases.  The `cl-compat'
  681. package defines a set of multiple-value macros that are compatible with
  682. the old CL package; again, they are heuristic in nature, but they are
  683. guaranteed to work in any case where the old package's macros worked.
  684. To avoid name collision with the "official" multiple-value facilities,
  685. the ones in `cl-compat' have capitalized names:  `Values',
  686. `Values-list', `Multiple-value-bind', etc.
  687.  
  688.    The functions `cl-floor', `cl-ceiling', `cl-truncate', and
  689. `cl-round' are defined by `cl-compat' to use the old-style
  690. multiple-value mechanism, just as they did in the old package.  The
  691. newer `floor*' and friends return their two results in a list rather
  692. than as multiple values.  Note that older versions of the old package
  693. used the unadorned names `floor', `ceiling', etc.; `cl-compat' cannot
  694. use these names because they conflict with Emacs 19 built-ins.
  695.  
  696. 
  697. File: cl,  Node: Porting Common Lisp,  Next: Function Index,  Prev: Old CL Compatibility,  Up: Top
  698.  
  699. Porting Common Lisp
  700. *******************
  701.  
  702. This package is meant to be used as an extension to Emacs Lisp, not as
  703. an Emacs implementation of true Common Lisp.  Some of the remaining
  704. differences between Emacs Lisp and Common Lisp make it difficult to
  705. port large Common Lisp applications to Emacs.  For one, some of the
  706. features in this package are not fully compliant with ANSI or Steele;
  707. *note Common Lisp Compatibility::..  But there are also quite a few
  708. features that this package does not provide at all.  Here are some
  709. major omissions that you will want watch out for when bringing Common
  710. Lisp code into Emacs.
  711.  
  712.    * Case-insensitivity.  Symbols in Common Lisp are case-insensitive
  713.      by default.  Some programs refer to a function or variable as
  714.      `foo' in one place and `Foo' or `FOO' in another.  Emacs Lisp will
  715.      treat these as three distinct symbols.
  716.  
  717.      Some Common Lisp code is written in all upper-case.  While Emacs
  718.      is happy to let the program's own functions and variables use this
  719.      convention, calls to Lisp builtins like `if' and `defun' will have
  720.      to be changed to lower-case.
  721.  
  722.    * Lexical scoping.  In Common Lisp, function arguments and `let'
  723.      bindings apply only to references physically within their bodies
  724.      (or within macro expansions in their bodies).  Emacs Lisp, by
  725.      contrast, uses "dynamic scoping" wherein a binding to a variable
  726.      is visible even inside functions called from the body.
  727.  
  728.      Variables in Common Lisp can be made dynamically scoped by
  729.      declaring them `special' or using `defvar'.  In Emacs Lisp it is
  730.      as if all variables were declared `special'.
  731.  
  732.      Often you can use code that was written for lexical scoping even
  733.      in a dynamically scoped Lisp, but not always.  Here is an example
  734.      of a Common Lisp code fragment that would fail in Emacs Lisp:
  735.  
  736.           (defun map-odd-elements (func list)
  737.             (loop for x in list
  738.                   for flag = t then (not flag)
  739.                   collect (if flag x (funcall func x))))
  740.           
  741.           (defun add-odd-elements (list x)
  742.             (map-odd-elements (function (lambda (a) (+ a x))) list))
  743.  
  744.      In Common Lisp, the two functions' usages of `x' are completely
  745.      independent.  In Emacs Lisp, the binding to `x' made by
  746.      `add-odd-elements' will have been hidden by the binding in
  747.      `map-odd-elements' by the time the `(+ a x)' function is called.
  748.  
  749.      (This package avoids such problems in its own mapping functions by
  750.      using names like `cl-x' instead of `x' internally; as long as you
  751.      don't use the `cl-' prefix for your own variables no collision can
  752.      occur.)
  753.  
  754.      *Note Lexical Bindings::, for a description of the `lexical-let'
  755.      form which establishes a Common Lisp-style lexical binding, and
  756.      some examples of how it differs from Emacs' regular `let'.
  757.  
  758.    * Common Lisp allows the shorthand `#'x' to stand for `(function
  759.      x)', just as `'x' stands for `(quote x)'.  In Common Lisp, one
  760.      traditionally uses `#'' notation when referring to the name of a
  761.      function.  In Emacs Lisp, it works just as well to use a regular
  762.      quote:
  763.  
  764.           (loop for x in y by #'cddr collect (mapcar #'plusp x))  ; Common Lisp
  765.           (loop for x in y by 'cddr collect (mapcar 'plusp x))    ; Emacs Lisp
  766.  
  767.      When `#'' introduces a `lambda' form, it is best to write out
  768.      `(function ...)' longhand in Emacs Lisp.  You can use a regular
  769.      quote, but then the byte-compiler won't know that the `lambda'
  770.      expression is code that can be compiled.
  771.  
  772.           (mapcar #'(lambda (x) (* x 2)) list)            ; Common Lisp
  773.           (mapcar (function (lambda (x) (* x 2))) list)   ; Emacs Lisp
  774.  
  775.      Lucid Emacs supports `#'' notation starting with version 19.8.
  776.  
  777.    * The "backquote" feature uses a different syntax in Emacs Lisp.
  778.  
  779.           (defmacro foo (v &rest body) `(let ((,v 0)) @,body))  ; Common Lisp
  780.           (defmacro foo (v &rest body) (` (let (((, v) 0)) (@, body)))  ; Emacs
  781.  
  782.    * Reader macros.  Common Lisp includes a second type of macro that
  783.      works at the level of individual characters.  For example, Common
  784.      Lisp implements the quote notation by a reader macro called `'',
  785.      whereas Emacs Lisp's parser just treats quote as a special case.
  786.      Some Lisp packages use reader macros to create special syntaxes
  787.      for themselves, which the Emacs parser is incapable of reading.
  788.  
  789.      The lack of reader macros, incidentally, is the reason behind
  790.      Emacs Lisp's unusual backquote syntax.  Since backquotes are
  791.      implemented as a Lisp package and not built-in to the Emacs
  792.      parser, they are forced to use a regular macro named ``' which is
  793.      used with the standard function/macro call notation.
  794.  
  795.    * Other syntactic features.  Common Lisp provides a number of
  796.      notations beginning with `#' that the Emacs Lisp parser won't
  797.      understand.  For example, `#| ... |#' is an alternate comment
  798.      notation, and `#+lucid (foo)' tells the parser to ignore the
  799.      `(foo)' except in Lucid Common Lisp.
  800.  
  801.    * Packages.  In Common Lisp, symbols are divided into "packages".
  802.      Symbols that are Lisp built-ins are typically stored in one
  803.      package; symbols that are vendor extensions are put in another,
  804.      and each application program would have a package for its own
  805.      symbols.  Certain symbols are "exported" by a package and others
  806.      are internal; certain packages "use" or import the exported symbols
  807.      of other packages.  To access symbols that would not normally be
  808.      visible due to this importing and exporting, Common Lisp provides
  809.      a syntax like `package:symbol' or `package::symbol'.
  810.  
  811.      Emacs Lisp has a single namespace for all interned symbols, and
  812.      then uses a naming convention of putting a prefix like `cl-' in
  813.      front of the name.  Some Emacs packages adopt the Common Lisp-like
  814.      convention of using `cl:' or `cl::' as the prefix.  However, the
  815.      Emacs parser does not understand colons and just treats them as
  816.      part of the symbol name.  Thus, while `mapcar' and `lisp:mapcar'
  817.      may refer to the same symbol in Common Lisp, they are totally
  818.      distinct in Emacs Lisp.  Common Lisp programs which refer to a
  819.      symbol by the full name sometimes and the short name other times
  820.      will not port cleanly to Emacs.
  821.  
  822.      Emacs Lisp does have a concept of "obarrays," which are
  823.      package-like collections of symbols, but this feature is not
  824.      strong enough to be used as a true package mechanism.
  825.  
  826.    * Keywords.  The notation `:test-not' in Common Lisp really is a
  827.      shorthand for `keyword:test-not'; keywords are just symbols in a
  828.      built-in `keyword' package with the special property that all its
  829.      symbols are automatically self-evaluating.  Common Lisp programs
  830.      often use keywords liberally to avoid having to use quotes.
  831.  
  832.      In Emacs Lisp a keyword is just a symbol whose name begins with a
  833.      colon; since the Emacs parser does not treat them specially, they
  834.      have to be explicitly made self-evaluating by a statement like
  835.      `(setq :test-not ':test-not)'.  This package arranges to execute
  836.      such a statement whenever `defun*' or some other form sees a
  837.      keyword being used as an argument.  Common Lisp code that assumes
  838.      that a symbol `:mumble' will be self-evaluating even though it was
  839.      never introduced by a `defun*' will have to be fixed.
  840.  
  841.    * The `format' function is quite different between Common Lisp and
  842.      Emacs Lisp.  It takes an additional "destination" argument before
  843.      the format string.  A destination of `nil' means to format to a
  844.      string as in Emacs Lisp; a destination of `t' means to write to
  845.      the terminal (similar to `message' in Emacs).  Also, format
  846.      control strings are utterly different; `~' is used instead of `%'
  847.      to introduce format codes, and the set of available codes is much
  848.      richer.  There are no notations like `\n' for string literals;
  849.      instead, `format' is used with the "newline" format code, `~%'.
  850.      More advanced formatting codes provide such features as paragraph
  851.      filling, case conversion, and even loops and conditionals.
  852.  
  853.      While it would have been possible to implement most of Common Lisp
  854.      `format' in this package (under the name `format*', of course), it
  855.      was not deemed worthwhile.  It would have required a huge amount
  856.      of code to implement even a decent subset of `format*', yet the
  857.      functionality it would provide over Emacs Lisp's `format' would
  858.      rarely be useful.
  859.  
  860.    * Vector constants use square brackets in Emacs Lisp, but `#(a b c)'
  861.      notation in Common Lisp.  To further complicate matters, Emacs 19
  862.      introduces its own `#(' notation for something entirely
  863.      different--strings with properties.
  864.  
  865.    * Characters are distinct from integers in Common Lisp.  The
  866.      notation for character constants is also different:  `#\A' instead
  867.      of `?A'.  Also, `string=' and `string-equal' are synonyms in Emacs
  868.      Lisp whereas the latter is case-insensitive in Common Lisp.
  869.  
  870.    * Data types.  Some Common Lisp data types do not exist in Emacs
  871.      Lisp.  Rational numbers and complex numbers are not present, nor
  872.      are large integers (all integers are "fixnums").  All arrays are
  873.      one-dimensional.  There are no readtables or pathnames; streams
  874.      are a set of existing data types rather than a new data type of
  875.      their own.  Hash tables, random-states, structures, and packages
  876.      (obarrays) are built from Lisp vectors or lists rather than being
  877.      distinct types.
  878.  
  879.    * The Common Lisp Object System (CLOS) is not implemented, nor is
  880.      the Common Lisp Condition System.
  881.  
  882.    * Common Lisp features that are completely redundant with Emacs Lisp
  883.      features of a different name generally have not been implemented.
  884.      For example, Common Lisp writes `defconstant' where Emacs Lisp
  885.      uses `defconst'.  Similarly, `make-list' takes its arguments in
  886.      different ways in the two Lisps but does exactly the same thing,
  887.      so this package has not bothered to implement a Common Lisp-style
  888.      `make-list'.
  889.  
  890.    * A few more notable Common Lisp features not included in this
  891.      package:  `compiler-let', `tagbody', `prog', `ldb/dpb',
  892.      `parse-integer', `cerror'.
  893.  
  894.    * Recursion.  While recursion works in Emacs Lisp just like it does
  895.      in Common Lisp, various details of the Emacs Lisp system and
  896.      compiler make recursion much less efficient than it is in most
  897.      Lisps.  Some schools of thought prefer to use recursion in Lisp
  898.      over other techniques; they would sum a list of numbers using
  899.      something like
  900.  
  901.           (defun sum-list (list)
  902.             (if list
  903.                 (+ (car list) (sum-list (cdr list)))
  904.               0))
  905.  
  906.      where a more iteratively-minded programmer might write one of
  907.      these forms:
  908.  
  909.           (let ((total 0)) (dolist (x my-list) (incf total x)) total)
  910.           (loop for x in my-list sum x)
  911.  
  912.      While this would be mainly a stylistic choice in most Common Lisps,
  913.      in Emacs Lisp you should be aware that the iterative forms are
  914.      much faster than recursion.  Also, Lisp programmers will want to
  915.      note that the current Emacs Lisp compiler does not optimize tail
  916.      recursion.
  917.  
  918.